package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.AlgoPesos;
import negModel.ControladorDeCombustible;
import negModelExcep.*;

public class ControladorDeCombustibleTest extends TestCase {

	ControladorDeCombustible controlador1,cont2;
	Element e;
	
	protected void setUp() throws Exception {
		super.setUp();
		controlador1 = new ControladorDeCombustible (1000,50,10,new String("Sistema de Inyeccion"),new AlgoPesos(100),true,10,7);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
	    
	    e = doc.createElement("parte");
		e.setAttribute("Clase","controladorDeCombus");
		
		e.setAttribute("maxVU", ""+ controlador1.getMaxVU());
		e.setAttribute("vidaUtil", ""+ controlador1.getVidaUtil());
		e.setAttribute("peso", ""+ controlador1.getPeso());
		e.setAttribute("aportevmax", ""+ controlador1.aportevmax());
		e.setAttribute("detalle", controlador1.getDetalle());
		e.setAttribute("precio", ""+(controlador1.getPrecio()).getMonto());
		e.setAttribute("removible", ""+controlador1.esRemovible());
		e.setAttribute("performance", ""+controlador1.getPerformance());
		
		e.setAttribute("porcentajeInyec", ""+controlador1.getPorcentajeInyec());
	    
	}

	public void testGastar() {
		try {
			controlador1.gastar();
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		assertEquals(999,controlador1.getVidaUtil(),0.1);
		
	}

	public void testFuncionar() {
		try {
			assertEquals(0.07,controlador1.funcionar(),0.1);
		} catch (eParteNoFunciona e) {
			fail("se elevo la excepcion");
		}
		
	}

	public void testSetPerformance() {
		controlador1.setPerformance(100);
		assertEquals(100,controlador1.getPerformance(),0.1);
	}

	public void testSetMaxVU() {
		controlador1.setMaxVU(100);
		assertEquals(100,controlador1.getMaxVU(),0.0);
	}

	public void testSetVidaUtil() {
		controlador1.setVidaUtil(-10);
		assertEquals(990,controlador1.getVidaUtil(),0.0);
	}

	public void testArreglar() {
		controlador1.arreglar();
		assertEquals(1000,controlador1.getVidaUtil(),0.0);
	}

	public void testSetPeso() {
		controlador1.setPeso(30);
		assertEquals(30,controlador1.getPeso(),0.0);
	}

	public void testSetAportevmax() {
		controlador1.setAportevmax(20);
		assertEquals(20,controlador1.aportevmax(),0.0);
	}

	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=controlador1.toXml(doc);
		
		assertEquals(controlador1.getPorcentajeInyec(),Float.parseFloat(e.getAttribute("porcentajeInyec")),0.0);
		
		assertEquals(controlador1.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((controlador1.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(controlador1.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(controlador1.getDetalle(),e.getAttribute("detalle"));
		assertEquals(controlador1.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(controlador1.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	public void testControladorDeCombus ()throws Exception {
		
		cont2=new ControladorDeCombustible(e);
				
		assertEquals(cont2.getPorcentajeInyec(),Float.parseFloat(e.getAttribute("porcentajeInyec")),0.0);
		
		assertEquals(cont2.getPerformance(),Integer.parseInt(e.getAttribute("performance")));
		assertEquals((cont2.getPrecio()).getMonto(),Double.parseDouble(e.getAttribute("precio")),0.0);
		assertEquals(cont2.aportevmax(),Float.parseFloat(e.getAttribute("aportevmax")),0.0);
		assertEquals(cont2.getDetalle(),e.getAttribute("detalle"));
		assertEquals(cont2.getPeso(),Double.parseDouble(e.getAttribute("peso")),0.0);
		assertEquals(cont2.getVidaUtil(),Integer.parseInt(e.getAttribute("vidaUtil")));
		
	}
	
	
}
